//
//  HomeViewController.m
//  EasyGo
//
//  Created by newhuidong on 16/1/18.
//  Copyright © 2016年 HuiDong. All rights reserved.
//
#define Start_X [UIScreen mainScreen].bounds.size.width/16          // 第一个按钮的X坐标
#define Start_Y 100.0f           // 第一个按钮的Y坐标
#define Width_Space [UIScreen mainScreen].bounds.size.width/5        // 2个按钮之间的横间距
#define Height_Space [UIScreen mainScreen].bounds.size.height/10-10      // 竖间距
#define Button_Height [UIScreen mainScreen].bounds.size.height/7    // 高
#define Button_Width [UIScreen mainScreen].bounds.size.width/3
#import "HomeViewController.h"
#import "MyProfileViewController.h"
#import "LoginViewController.h"
#import "MyMeansViewController.h"
@interface HomeViewController (){
    
    UIImageView *navBarHairlineImageView;
    
}
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"主页";
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:0/255.0 green:128/255.0 blue:255/255.0f alpha:1];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    
    UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:@"" forState:0];
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    [self addButtonS];

}

#pragma 隐藏导航栏下面的一条线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}


//创建按钮=======
-(void)addButtonS{
    
    
        for (int i = 0 ; i < 6; i++) {
            NSInteger index = i % 3;
            NSInteger page = i / 3;
            // 圆角按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.layer.cornerRadius=4;
            
            button.frame = CGRectMake(page * (Button_Width + Width_Space) + Start_X, index  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
            [button setBackgroundColor:[UIColor redColor]];
            button.tag=i;
            NSArray * array=[NSArray arrayWithObjects:@"联系客服",@"检测更新",@"关于易行",@"消息中心",@"使用指南",@"我的资料", nil];
            if (button.tag==0) {
                
                [button setBackgroundColor:[UIColor colorWithRed:253/255.0f green:152/255.0f blue:44/255.0f alpha:1]];
                [button setImage:[UIImage imageNamed:@"kefu"] forState:UIControlStateNormal];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
                [button setContentMode:UIViewContentModeCenter];
                
            }else if (button.tag==1){
                
                [button setBackgroundColor:[UIColor colorWithRed:180/255.0f green:111/255.0f blue:245/255.0f alpha:1]];
                [button setImage:[UIImage imageNamed:@"gengxin"] forState:0];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
            }else if (button.tag==2){
                
                [button setBackgroundColor:[UIColor colorWithRed:244/255.0f green:172/255.0f blue:10/255.0f alpha:1]];
                [button setImage:[UIImage imageNamed:@"about"] forState:0];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
            }else if (button.tag==3){
                
                [button setBackgroundColor:[UIColor colorWithRed:124/255.0f green:203/255.0f blue:60/255.0f alpha:1]];
                [button setImage:[UIImage imageNamed:@"message"] forState:0];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];

                
            }else if (button.tag==4){
                
                [button setBackgroundColor:[UIColor colorWithRed:252/255.0f green:89/255.0f blue:74/255.0f alpha:1]];
                [button setImage:[UIImage imageNamed:@"zhinan"] forState:0];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
                
            }
            
            if (button.tag==5) {
                
                [button setBackgroundColor:[UIColor colorWithRed:21/255.0f green:167/255.0f blue:226/255.0f alpha:1]];
                //[button setBackgroundImage:[UIImage imageNamed:@"ziliao"] forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"ziliao"] forState:UIControlStateNormal];
                [button setImageEdgeInsets:UIEdgeInsetsMake(10, 20, 10, 20)];
                
            }
            
            UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(page * (Button_Width + Width_Space) + Start_X, index  * (Button_Height + Height_Space)+Start_Y+Button_Height+5, Button_Width, 30)];
            
            //label.backgroundColor=[UIColor blackColor];
            label.text=array[i];
            label.textColor=[UIColor darkGrayColor];
            label.textAlignment=NSTextAlignmentCenter;
            
            [self.view addSubview:label];
            
            
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.view  addSubview:button];
        }
    
    CGRect frame =[UIScreen mainScreen].bounds;
    
    
    UIButton * exitBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    
    exitBtn.frame=CGRectMake(20, frame.size.height-70, frame.size.width-40, 40);
    
    [exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitBtn setBackgroundColor:[UIColor colorWithRed:0/255.0 green:128/255.0 blue:255/255.0 alpha:1]];
    [exitBtn setTitleColor:[UIColor whiteColor] forState:0];
    exitBtn.layer.cornerRadius=3;
    exitBtn.titleLabel.font=[UIFont systemFontOfSize:17];
    [exitBtn addTarget:self action:@selector(tuichu:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:exitBtn];
    
    
}
#pragma -------------退出----------------
-(void)tuichu:(UIButton*)button{
    UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"主淫!" message:@"真的要离开我吗/(ㄒoㄒ)/~~" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction * yes=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    [alert addAction:cancel];
    [alert addAction:yes];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}
-(void)buttonClick:(UIButton*)button{
    
    
    NSLog(@"%ld",(long)button.tag);
    if (button.tag==5) {
        
        UIStoryboard * story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        MyProfileViewController * my=[story instantiateViewControllerWithIdentifier:@"MyProfileViewController"];
        
        [self.navigationController pushViewController:my animated:YES];
    }else{
        
        
        UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"近期开放" message:@"敬请期待!" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * action =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
}
@end
